﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraCharts;

namespace TaxiDispatchClient
{
    public partial class PhoneUserReportForm : DevExpress.XtraEditors.XtraForm
    {
        public PhoneUserReportForm()
        {
            InitializeComponent();   
            //默认按月统计每天登录人次
            dateEdit1.DateTime = DateTime.Now;

            int[] range = new int[]{2,3,4};
            DataSet ds = MainForm.PositionService.PhoneUserGetReportByRecommender(range,"");

        }
        private void Form_Load(object sender, EventArgs e)
        {
            _thread = new Thread(BindData);
            _thread.Start();
        }
        private DataSet _ds;
        private Thread _thread;

        private void btn_getResult_Click(object sender, EventArgs e)
        {
            _thread = new Thread(BindData);
            _thread.Start();
        }

        private void BindData()
        {
            if (comboBox_type.SelectedIndex == 0) //按用户登录次数统计
            {
                int[] rangValue = new int[] { 5, 10, 20, 50, 100 };
                _ds = MainForm.PositionService.PhoneGetReportByLoginCount(rangValue, "");
                if (_ds == null || _ds.Tables.Count == 0 || _ds.Tables[0].Rows.Count == 0)
                {
                     BeginInvoke(new MethodInvoker(() =>
                        {
                            chartControl1.DataSource = null;
                        }));
                    return;
                }
                Series series = new Series("登录次数统计", ViewType.Bar);
                BeginInvoke(new MethodInvoker(() =>
                {
                    chartControl1.Series.Clear();
                    chartControl1.Series.Add(series);
                    XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
                    diagram.AxisX.Title.Text = "登录次数";
                    diagram.AxisY.Title.Text = "用户数量(人)";
                    for (int i = 0; i < rangValue.Length; i++)
                    {
                        SeriesPoint seriesPoint = new SeriesPoint();
                        //new SeriesPoint("载客率", new double[] { carrypert });
                        int count = int.Parse(_ds.Tables[0].Rows[0][i].ToString());
                        if (i == 0)
                        {
                            seriesPoint.Argument = "1至" + rangValue[0].ToString() + "次";
                        }                       
                        else
                        {
                            seriesPoint.Argument = rangValue[i - 1].ToString() + "至" +
                            rangValue[i].ToString() + "次";
                        }
                        seriesPoint.Values = new double[] { count };
                        chartControl1.Series[0].Points.Add(seriesPoint);
                    }
                    //加入最后一列
                    SeriesPoint seriesPoint2 = new SeriesPoint();
                    seriesPoint2.Argument = rangValue[rangValue.Length - 1].ToString() + "次以上";
                    int count2 = int.Parse(_ds.Tables[0].Rows[0][rangValue.Length].ToString());
                    seriesPoint2.Values = new double[] { count2 };
                    chartControl1.Series[0].Points.Add(seriesPoint2);
                }));

            }       
            else if (comboBox_type.SelectedIndex == 1)//按交易次数统计
            {
                int[] rangValue = new int[] { 5, 10, 20, 50, 100 };
                _ds = MainForm.PositionService.PhoneUserGetReportByTradeTimes(rangValue, "");
                if (_ds == null || _ds.Tables.Count == 0 || _ds.Tables[0].Rows.Count == 0)
                {
                    chartControl1.DataSource = null;
                    return;
                }
                BeginInvoke(new MethodInvoker(() =>
                                                  {
                                                      Series series = new Series("用户人数", ViewType.Bar);
                                                      chartControl1.Series.Clear();
                                                      chartControl1.Series.Add(series);
                                                      XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
                                                      diagram.AxisX.Title.Text = "招车次数";
                                                      diagram.AxisY.Title.Text = "用户数量(人)";
                                                      SeriesPoint seriesPoint0 = new SeriesPoint();
                                                      seriesPoint0.Argument = "0次";
                                                      int count0 = int.Parse(_ds.Tables[0].Rows[0][0].ToString());
                                                      seriesPoint0.Values = new double[] { count0 };
                                                      chartControl1.Series[0].Points.Add(seriesPoint0);
                                                      for (int i = 0; i < rangValue.Length; i++)
                                                      {
                                                          SeriesPoint seriesPoint = new SeriesPoint();
                                                          //new SeriesPoint("载客率", new double[] { carrypert });
                                                          int count = int.Parse(_ds.Tables[0].Rows[0][i + 1].ToString());
                                                          if (i == 0)
                                                          {
                                                              seriesPoint.Argument = "1至" + rangValue[0].ToString() + "次";
                                                          }                                                          
                                                          else
                                                          {
                                                              seriesPoint.Argument = rangValue[i - 1].ToString() + "至" +
                                                              rangValue[i].ToString() + "次";
                                                          }
                                                          seriesPoint.Values = new double[] { count };
                                                          chartControl1.Series[0].Points.Add(seriesPoint);
                                                      }
                                                      //加入最后一列
                                                      SeriesPoint seriesPoint2 = new SeriesPoint();
                                                      seriesPoint2.Argument = rangValue[rangValue.Length - 1].ToString() + "次以上";
                                                      int count2 = int.Parse(_ds.Tables[0].Rows[0][rangValue.Length + 1].ToString());
                                                      seriesPoint2.Values = new double[] { count2 };
                                                      chartControl1.Series[0].Points.Add(seriesPoint2);
                                                  }));

            }
            #region  按推荐人统计
            //else if (comboBox_type.SelectedIndex == 2)
            // {
            //     int[] rangValue = new int[] { 5, 10, 20, 50, 100 };
            //     _ds = MainForm.PositionService.PhoneUserGetReportByRecommender(rangValue, "");
            //     if (_ds == null || _ds.Tables.Count == 0 || _ds.Tables[0].Rows.Count == 0)
            //     {
            //         chartControl1.DataSource = null;
            //         return;
            //     }
            //     BeginInvoke(new MethodInvoker(() =>
            //     {
            //         Series series = new Series("推荐人", ViewType.Bar);
            //         chartControl1.Series.Clear();
            //         chartControl1.Series.Add(series);
            //         XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            //         diagram.AxisX.Title.Text = "推荐用户人次";
            //         diagram.AxisY.Title.Text = "推荐人数量";
                     
            //         for (int i = 0; i < rangValue.Length; i++)
            //         {
            //             SeriesPoint seriesPoint = new SeriesPoint();
            //             //new SeriesPoint("载客率", new double[] { carrypert });
            //             int count = int.Parse(_ds.Tables[0].Rows[0][i].ToString());
            //             if (i == 0)
            //             {
            //                 seriesPoint.Argument = "1至" + rangValue[0].ToString() + "人次";
            //             }
            //             else
            //             {
            //                 seriesPoint.Argument = rangValue[i - 1].ToString() + "至" +
            //                 rangValue[i].ToString() + "人次";
            //             }
            //             seriesPoint.Values = new double[] { count };
            //             chartControl1.Series[0].Points.Add(seriesPoint);
            //         }
            //         //加入最后一列
            //         SeriesPoint seriesPoint2 = new SeriesPoint();
            //         seriesPoint2.Argument = rangValue[rangValue.Length - 1].ToString() + "次以上";
            //         int count2 = int.Parse(_ds.Tables[0].Rows[0][rangValue.Length ].ToString());
            //         seriesPoint2.Values = new double[] { count2 };
            //         chartControl1.Series[0].Points.Add(seriesPoint2);
                     
            //         //SeriesPoint seriesPoint0 = new SeriesPoint();                     
            //         //seriesPoint0.Argument = "无推荐人用户数";
            //         //int count0 = int.Parse(_ds.Tables[0].Rows[0][rangValue.Length+1].ToString());
            //         //seriesPoint0.Values = new double[] { count0 };
            //         //chartControl1.Series[0].Points.Add(seriesPoint0);
            //     }));
            // }
            #endregion

            #region 按时间段登录人次统计  有bug
            else if (comboBox_type.SelectedIndex == 2) //按时间段登录人次统计
            {
                DateTime dateTime = dateEdit1.DateTime;
                //DataSet ds = null;
                StringBuilder strWhere = new StringBuilder();
                int dateType = combox_dateType.SelectedIndex + 1;
                XYDiagram diagram = (XYDiagram)chartControl1.Diagram;

                switch (dateType)
                {
                    case 1:
                        strWhere.Append("FirstLoginTime betWeen '");
                        strWhere.Append(dateTime.ToString("yyyy-MM-dd"));
                        strWhere.Append(" 00:00:00'");
                        strWhere.Append(" and '");
                        strWhere.Append(dateTime.ToString("yyyy-MM-dd"));
                        strWhere.Append(" 23:59:59'");
                        diagram.AxisX.Title.Text = "时间（小时）";
                        break;
                    case 2:
                        DateTime firstDay = new DateTime(dateTime.Year, dateTime.Month, 1);
                        DateTime lastDay = firstDay.AddMonths(1).AddDays(-1);
                        strWhere.Append("FirstLoginTime Between '");
                        strWhere.Append(firstDay.ToString("yyyy-MM-dd"));
                        strWhere.Append("' and '");
                        strWhere.Append(lastDay.ToString("yyyy-MM-dd"));
                        strWhere.Append(" 23:59:59");
                        strWhere.Append("'");
                        diagram.AxisX.Title.Text = "时间（日期）";
                        break;
                    case 3:
                        firstDay = new DateTime(dateTime.Year, 1, 1);
                        lastDay = new DateTime(dateTime.Year, 12, 31);
                        strWhere.Append("FirstLoginTime Between '");
                        strWhere.Append(firstDay.ToString("yyyy-MM-dd"));
                        strWhere.Append("' and '");
                        strWhere.Append(lastDay.ToString("yyyy-MM-dd"));
                        strWhere.Append(" 23:59:59");
                        strWhere.Append("'");
                        diagram.AxisX.Title.Text = "时间（月份）";
                        break;
                }
                try
                {
                    _ds = MainForm.PositionService.PhoneUserGetReportByTime(dateType,
                        strWhere.ToString());


                    BeginInvoke(new MethodInvoker(() =>
                                                      {
                                                          chartControl1.DataSource = _ds.Tables[0];
                                                          Series series = new Series("登录人次", ViewType.StackedSplineArea);
                                                          chartControl1.Series.Clear();
                                                          chartControl1.Series.Add(series);
                                                          //  XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
                                                          // diagram.AxisX.Title.Text = "时间";
                                                          diagram.AxisY.Title.Text = "用户数量(人)";
                                                          chartControl1.Series[0].ArgumentDataMember = "DT";
                                                          chartControl1.Series[0].ValueDataMembers.AddRange(new string[] { "LoginCount" });
                                                      }));
                }
                catch (Exception ex)
                {
                    Common.CommonRoutine.HandlingExceptions(ex);
                }
            }
            #endregion
        }

        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox_type.SelectedIndex==0)  // 按用户登录次数统计
            {
                panel_time.Visible = false;
                //panel_count.Visible = true;
                //panel_count.Location = panel_time.Location;
            }
            else if (comboBox_type.SelectedIndex == 2) // 按规定时间内登录人次统计
            {
                panel_time.Visible = true;
                //panel_count.Visible = false;
            }
            else if (comboBox_type.SelectedIndex == 1) //按用户交易次数统计
            {
                panel_time.Visible = false;
            }
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserReportForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.Close();
            }
        }
        #endregion

        private void PhoneUserReportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_thread != null)
            {
                _thread.Abort();
            }
        }





    }
}